package edu.bw.web.permission;

import cn.hutool.json.JSONUtil;
import edu.bw.common.CommonResult;
import edu.bw.dto.UpdateRoleForm;
import edu.bw.service.RoleService;
import edu.bw.service.impl.RoleServiceImpl;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet("/role/update")
public class UpdateRoleServlet extends HttpServlet {
    RoleService roleService = new RoleServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //{"id":13,"roleName":"测试222","permissions":[27,30,31,32],"desc":"测试111","changed":true}
        //1.解析前端请求的数据
        String reqJSON = request.getReader().readLine();
        UpdateRoleForm updateRoleForm = JSONUtil.toBean(reqJSON, UpdateRoleForm.class);

        //2.调用service层处理数据
        Integer rows = roleService.update(updateRoleForm);

        //3.向前端响应数据
        response.getWriter().write(JSONUtil.toJsonStr(CommonResult.ok().put("rows", rows)));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
